
import { getAllModalComponent } from './component'
// 查找积木指定类型的父级积木
// export function findParentByType1 (block, type, res = null) {
//   const parent = block.getParent()
//   if (parent && parent.type && parent.type !== 'root_function_expression' && parent.type !== type) {
//     return findParentByType(parent, type, res)
//   } else if (parent?.type === type) {
//     return parent
//   }
// }

export function findParentByType (block, type) {
  var temp = []
  const forFn = function (block, type) {
    const parent = block.getParent()
    if (parent && parent.type) {
      if (parent.type === type) {
        temp.push(parent)
      }
      forFn(parent, type)
    }
  }
  forFn(block, type)
  return temp
}

// 获取当前的context的具体值
export function getContextText (block, comKey) {
  let res = 'context'
  const modalComList = getAllModalComponent()
  const modalRes = modalComList.find(item => item.key === comKey) // modal中的组件
  if (!modalRes) return res
  // 回调所在的block
  const cbParents = findParentByType(block, 'callback_function_expression') || []
  const flag = cbParents.find(cbParent => {
    const rootBlock = cbParent.getParent()
    // const modalParentTypes = ['open_modal', 'close_modal']
    const modalParentTypes = ['open_modal', 'close_modal']
    if (rootBlock && modalParentTypes.includes(rootBlock.type)) {
      // 打开弹窗的回调
      const openCb = rootBlock.getInputTargetBlock('modal_open')
      if (openCb && openCb.id === cbParent.id) {
        return true
      }
    }
  })
  if (flag) res = 'tctx'
  // if (cbParent) {
  //   // 获取模态框的block
  //   const rootBlock = cbParent.getParent()
  //   // const modalParentTypes = ['open_modal', 'close_modal']
  //   const modalParentTypes = ['open_modal', 'close_modal']
  //   if (rootBlock && modalParentTypes.includes(rootBlock.type)) {
  //     // 打开弹窗的回调
  //     const openCb = rootBlock.getInputTargetBlock('modal_open')
  //     if (openCb && openCb.id === cbParent.id) {
  //       res = 'tctx'
  //     }
  //   }
  // }
  return res
}
